package com.open.core.store

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences


/**************************************************************
 *
 *
 * 存储需要监听数据状态的值
 *
 * @author :guangjing.feng.o
 * @createTime: 2021/6/3.
 * @modifyTime:
 * @modifyAuthor:
 * @description:
 *
 ************************************************************/
class SharePreferencesHelper {

    @SuppressLint("WrongConstant")
    constructor(context: Context) {
        this.context = context
        mPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_APPEND)
    }

    companion object {
        @JvmStatic
        private val TAG = "SharePreferencesHelper"

        //sp文件的名字
        @JvmStatic
        private val SP_NAME = "sp"

        @JvmStatic
        private var mSPHelper: SharePreferencesHelper? = null

        @JvmStatic
        fun getInstance(context: Context): SharePreferencesHelper? {
            if (mSPHelper == null) synchronized(SharePreferencesHelper::class.java) {
                if (mSPHelper == null) {
                    mSPHelper = SharePreferencesHelper(context)
                }
            }
            return mSPHelper
        }

    }

    private var mPreferences: SharedPreferences? = null

    private var mEditor: SharedPreferences.Editor? = null


    private var context: Context? = null


    fun putString(key: String?, value: String?): Boolean {
        mEditor = mPreferences!!.edit()
        mEditor!!.putString(key, value)
        return mEditor!!.commit()
    }

    fun getString(key: String?): String? {
        return mPreferences!!.getString(key, "")
    }

    fun getString(key: String?, defValue: String?): String? {
        return mPreferences!!.getString(key, defValue)
    }

    fun removeString(key: String?): Boolean {
        mEditor = mPreferences!!.edit()
        mEditor!!.remove(key)
        return mEditor!!.commit()
    }


    fun putInt(key: String?, value: Int): Boolean {
        mEditor = mPreferences!!.edit()
        mEditor!!.putInt(key, value)
        return mEditor!!.commit()
    }

    fun getInt(key: String?): Int {
        return mPreferences!!.getInt(key, 0)
    }

    fun getInt(key: String?, defValue: Int): Int {
        return mPreferences!!.getInt(key, defValue)
    }


    fun putLong(key: String?, value: Long): Boolean {
        mEditor = mPreferences!!.edit()
        mEditor!!.putLong(key, value)
        return mEditor!!.commit()
    }

    fun getLong(key: String?): Long {
        return mPreferences!!.getLong(key, 0)
    }

    fun getLong(key: String?, defValue: Long): Long {
        return mPreferences!!.getLong(key, defValue)
    }


    fun putBoolean(key: String?, value: Boolean): Boolean {
        mEditor = mPreferences!!.edit()
        mEditor!!.putBoolean(key, value)
        return mEditor!!.commit()
    }


    fun getBoolean(key: String?): Boolean {
        return mPreferences!!.getBoolean(key, false)
    }

    fun getBoolean(key: String?, defValue: Boolean): Boolean {
        return mPreferences!!.getBoolean(key, defValue)
    }

    fun clearData() {
        mPreferences = context?.getSharedPreferences(SP_NAME, 0)
        mPreferences!!.edit().clear().commit()
    }


}